﻿using System;
using System.ComponentModel;
using System.Reflection;
using System.Runtime.Serialization;

namespace ConsoleApp.enums
{
    public enum ExecuteHeightModeEnums
    {
        // WGS84：椭球高模式
        [EnumMember(Value = "WGS84")]
        [Description("WGS84")]
        WGS84,
        //  relativeToStartPoint：相对起飞点高度模式
        [EnumMember(Value = "relativeToStartPoint")]
        [Description("相对起飞点高度模式")]
        RelativeToStartPoint,
        // realTimeFollowSurface: 使用实时仿地模式，仅支持M3E/M3T/M3M
        [EnumMember(Value = "realTimeFollowSurface")]
        [Description("使用实时仿地模式")]
        RealTimeFollowSurface,
    }
    /// <summary>
    /// 动作触发器类型
    /// </summary>
    public enum ActionTriggerTypeEnums
    {
        // * 注：“betweenAdjacentPoints”需配合动作"gimbalEvenlyRotate"使用，
        // “multipleTiming” 配合动作 “takePhoto” 即可实现等时间隔拍照，
        // “multipleDistance” 配合动作 “takePhoto” 即可实现等距离间隔拍照。

        // reachPoint：到达航点时执行
        [EnumMember(Value = "reachPoint")]
        [Description("到达航点时执行")]
        ReachPoint,
        // betweenAdjacentPoints：航段触发，均匀转云台
        [EnumMember(Value = "betweenAdjacentPoints")]
        [Description("航段触发，均匀转云台")]
        BetweenAdjacentPoints,
        // multipleTiming：等时触发
        [EnumMember(Value = "multipleTiming")]
        [Description("等时触发")]
        MultipleTiming,
        // multipleDistance：等距触发
        [EnumMember(Value = "multipleDistance")]
        [Description("等距触发")]
        MultipleDistance,
    }
    /// <summary>
    /// 负载扫描模式
    /// </summary>
    public enum ScanningModeEnums
    {
        // repetitive：重复扫描
        [EnumMember(Value = "repetitive")]
        [Description("重复扫描")]
        Repetitive,
        // nonRepetitive：非重复扫描
        [EnumMember(Value = "nonRepetitive")]
        [Description("非重复扫描")]
        NonRepetitive,
    }
    /// <summary>
    /// 激光雷达回波模式
    /// </summary>
    public enum ReturnModeEnums
    {
        // singleReturnStrongest：单回波
        [EnumMember(Value = "singleReturnStrongest")]
        [Description("单回波")]
        SingleReturnStrongest,
        //         dualReturn：双回波
        [EnumMember(Value = "dualReturn")]
        [Description("双回波")]
        DualReturn,
        // tripleReturn：三回波
        [EnumMember(Value = "tripleReturn")]
        [Description("手动控制云台俯仰角")]
        TripleReturn,
    }
    /// <summary>
    /// 负载对焦模式
    /// </summary>
    public enum MeteringModeEnums
    {
        // average：全局测光
        [EnumMember(Value = "average")]
        [Description("全局测光")]
        Average,
        // spot：点测光
        [EnumMember(Value = "spot")]
        [Description("固定为用户设置的俯仰角")]
        Spot,
    }
    /// <summary>
    /// 负载对焦模式
    /// </summary>
    public enum FocusModeEnums
    {
        // firstPoint：首个航点自动对焦
        [EnumMember(Value = "firstPoint")]
        [Description("手动控制云台俯仰角")]
        FirstPoint,
        // custom：标定对焦值对焦
        [EnumMember(Value = "custom")]
        [Description("固定为用户设置的俯仰角")]
        Custom,
    }
    /// <summary>
    /// 飞行器偏航角转动方向
    /// </summary>
    public enum WaypointHeadingPathModeEnums
    {
        // clockwise：顺时针旋转飞行器偏航角
        [EnumMember(Value = "clockwise")]
        [Description("顺时针旋转飞行器偏航角")]
        Clockwise,
        // manually：手动控制。飞行器在飞至下一航点的过程中，用户可以手动控制飞行器机头朝向
        [EnumMember(Value = "counterClockwise")]
        [Description("逆时针旋转飞行器偏航角")]
        CounterClockwise,
        // fixed：锁定当前偏航角。飞行器机头保持执行完航点动作后的飞行器偏航角飞至下一航点
        [EnumMember(Value = "followBadArc")]
        [Description("沿最短路径旋转飞行器偏航角")]
        FollowBadArc,
    }
    /// <summary>
    /// 飞行器偏航角模式
    /// </summary>
    public enum WaypointHeadingModeEnums
    {
        // followWayline：沿航线方向。飞行器机头沿着航线方向飞至下一航点
        [EnumMember(Value = "followWayline")]
        [Description("沿航线方向")]
        FollowWayline,
        // manually：手动控制。飞行器在飞至下一航点的过程中，用户可以手动控制飞行器机头朝向
        [EnumMember(Value = "manually")]
        [Description("手动控制")]
        Manually,
        // fixed：锁定当前偏航角。飞行器机头保持执行完航点动作后的飞行器偏航角飞至下一航点
        [EnumMember(Value = "fixed")]
        [Description("锁定当前偏航角")]
        Fixed,
        // smoothTransition：自定义。通过“wpml:waypointHeadingAngle”给定某航点的目标偏航角，并在航段飞行过程中均匀过渡至下一航点的目标偏航角。
        [EnumMember(Value = "smoothTransition")]
        [Description("自定义")]
        SmoothTransition,
        // towardPOI：朝向兴趣点
        [EnumMember(Value = "towardPOI")]
        [Description("自定义")]
        TowardPOI,
    }
    /// <summary>
    /// 全局航点类型（全局航点转弯模式）
    /// </summary>
    public enum WaypointTurnModeEnums
    {
        [EnumMember(Value = "coordinateTurn")]
        [Description("协调转弯，不过点，提前转弯")]
        CoordinateTurn,

        [EnumMember(Value = "toPointAndStopWithDiscontinuityCurvature")]
        [Description("直线飞行，飞行器到点停")]
        ToPointAndStopWithDiscontinuityCurvature,

        [EnumMember(Value = "toPointAndStopWithContinuityCurvature")]
        [Description("曲线飞行，飞行器到点停")]
        ToPointAndStopWithContinuityCurvature,

        [EnumMember(Value = "toPointAndPassWithContinuityCurvature")]
        [Description("曲线飞行，飞行器过点不停停")]
        ToPointAndPassWithContinuityCurvature,
    }
    /// <summary>
    /// 云台俯仰角控制模式
    /// </summary>
    public enum GimbalPitchModeEnums
    {
        // manual：手动控制。飞行器从一个航点飞向下一个航点的过程中，支持用户手动控制云台的俯仰角度。若无用户控制，则保持飞离航点时的云台俯仰角度。
        [EnumMember(Value = "manual")]
        [Description("手动控制云台俯仰角")]
        Manual,
        
        // usePointSetting：依照每个航点设置。飞行器从一个航点飞向下一个航点的过程中，云台俯仰角均匀过渡至下一个航点的俯仰角
        [EnumMember(Value = "usePointSetting")]
        [Description("依照每个航点设置")]
        UsePointSetting,
    }
    /// <summary>
    /// 航点高程参考平面
    /// </summary>
    public enum HeightModeEnums
    {
       
        [EnumMember(Value = "EGM96")]
        [Description("使用海拔高编辑")]
        EGM96,

        [EnumMember(Value = "relativeToStartPoint")]
        [Description("使用相对点的高度进行编辑")]
        RelativeToStartPoint,

        /// <summary>
        /// aboveGroundLevel：使用地形数据，AGL下编辑(仅支持司空2平台)
        /// </summary>
        [EnumMember(Value = "aboveGroundLevel")]
        [Description("使用地形数据")]
        AboveGroundLevel,
        /// <summary>
        /// 使用实时仿地模式（仅用于建图航拍模版），仅支持M3E/M3T/M3M机型
        /// </summary>
        [EnumMember(Value = "realTimeFollowSurface")]
        [Description("使用实时仿地模式")]
        RealTimeFollowSurface
    }
    /// <summary>
    /// 文件夹模板类型枚举
    /// </summary>
    public enum FolderTemplateTypeEnums
    {
        [EnumMember(Value = "waypoint")]
        [Description("航点飞行")]
        Waypoint,

        [EnumMember(Value = "mapping2d")]
        [Description("建图航拍")]
        Mapping2D,

        [EnumMember(Value = "mapping3d")]
        [Description("倾斜摄影")]
        Mapping3D,

        [EnumMember(Value = "mappingStrip")]
        [Description("航带飞行")]
        MappingStrip
    }

    /// <summary>
    /// 失控动作类型枚举
    /// </summary>
    public enum ExecuteRCLostActiontEnums
    {
        
        // goBack：返航。飞行器从失控位置飞向起飞点
        [EnumMember(Value = "返航")]
        [Description("继续执行航线")]
        GoBack,

        // landing：降落。飞行器从失控位置原地降落
        [EnumMember(Value = "landing")]
        [Description("降落")]
        Landing,

        // hover：悬停。飞行器从失控位置悬停
        [EnumMember(Value = "hover")]
        [Description("悬停")]
        Hover,

    }

    /// <summary>
    /// 失控是否继续执行航线枚举
    /// </summary>
    public enum ExitOnRCLostEnums
    {
        
        // goContinue：继续执行航线。
        [EnumMember(Value = "goContinue")]
        [Description("继续执行航线")]
        GoContinue,

        // executeLostAction：退出航线，执行失控动作。
        [EnumMember(Value = "executeLostAction")]
        [Description("退出航线，执行失控动作")]
        ExecuteLostAction,
       
    }

    /// <summary>
    /// 航线结束动作枚举
    /// </summary>
    public enum FinishActionEnums
    {
        // goHome：飞行器完成航线任务后，退出航线模式并返航。
        [EnumMember(Value = "goHome")]
        [Description("退出航线模式并返航")]
        GoHome,

        // noAction：飞行器完成航线任务后，退出航线模式。
        [EnumMember(Value = "noAction")]
        [Description("退出航线模式")]
        NoAction,
        //autoLand：飞行器完成航线任务后，退出航线模式并原地降落。
        [EnumMember(Value = "autoLand")]
        [Description("退出航线模式并原地降落")]
        AutoLand,
        // gotoFirstWaypoint：飞行器完成航线任务后，立即飞向航线起始点，到达后退出航线模式。* 注：以上动作执行过程，若飞行器退出了航线模式且进入失控状态，则会优先执行失控动作。
        [EnumMember(Value = "gotoFirstWaypoint")]
        [Description("立即飞向航线起始点，到达后退出航线模式")]
        GotoFirstWaypoint,

    }
    /// <summary>
    /// 飞向首航点模式枚举
    /// </summary>
    public enum FlyToWaylineModeEnums
    {
        // safely：安全模式
        //（M300）飞行器起飞，上升至首航点高度，再平飞至首航点。如果首航点低于起飞点，则起飞后平飞至首航点上方再下降。
        //（M30）飞行器起飞，上升至首航点高度，再平飞至首航点。如果首航点低于“安全起飞高度”，则起飞至“安全起飞高度”后，平飞至首航点上方再下降。注意“安全起飞高度”仅在飞行器未起飞时生效。
        [EnumMember(Value = "safely")]
        [Description("安全模式")]
        Safely,

        // pointToPoint：倾斜飞行模式
        //（M300）飞行器起飞后，倾斜飞到首航点。
        //（M30）飞行器起飞至“安全起飞高度”，再倾斜爬升至首航点。如果首航点高度低于“安全起飞高度”，则先平飞后下降。
        [EnumMember(Value = "pointToPoint")]
        [Description("倾斜飞行模式")]
        PointToPoint,
    }

    public enum DeviceDomainEnums
    {
        [EnumMember(Value = "0")]
        [Description("DRONE")]
        Drone = 0,

        [EnumMember(Value = "1")]
        [Description("PAYLOAD")]
        Payload = 1,

        [EnumMember(Value = "2")]
        [Description("REMOTER_CONTROL")]
        RemoterControl = 2,

        [EnumMember(Value = "3")]
        [Description("DOCK")]
        Dock = 3
    }

    public enum DeviceTypeEnums
    {
        // 机场
        [EnumMember(Value = "1")]
        DOCK = (1),

        [EnumMember(Value = "2")]
        DOCK2 = (2),

        [EnumMember(Value = "3")]
        DOCK3 = (3),

        // 遥控器
        [EnumMember(Value = "56")]
        RC = (56),

        [EnumMember(Value = "119")]
        RC_PLUS = (119),

        [EnumMember(Value = "174")]
        RC_PLUS2 = (174),

        [EnumMember(Value = "144")]
        RC_PRO = (144),

        // 飞行器
        [EnumMember(Value = "103")]
        M400 = (103),

        [EnumMember(Value = "89")]
        M350 = (89),

        [EnumMember(Value = "60")]
        M300 = (60),

        [EnumMember(Value = "67")]
        M30 = (67),

        [EnumMember(Value = "67")]
        M30T = (67),

        [EnumMember(Value = "77")]
        M3E = (77),

        [EnumMember(Value = "77")]
        M3T = (77),

        [EnumMember(Value = "77")]
        M3TA = (77),

        [EnumMember(Value = "91")]
        M3D = (91),

        [EnumMember(Value = "91")]
        M3TD = (91),

        [EnumMember(Value = "100")]
        M4D = (100),

        [EnumMember(Value = "100")]
        M4TD = (100),

        // 相机

        [EnumMember(Value = "99")]
        M4E_CAMERA = (99),

        [EnumMember(Value = "99")]
        M4T_CAMERA = (99),

        [EnumMember(Value = "20")]
        Z30 = (20),

        [EnumMember(Value = "26")]
        XT2 = (26),

        [EnumMember(Value = "39")]
        FPV = (39),

        [EnumMember(Value = "41")]
        XTS = (41),

        [EnumMember(Value = "42")]
        H20 = (42),

        [EnumMember(Value = "43")]
        H20T = (43),

        [EnumMember(Value = "50")]
        P1 = (50),

        [EnumMember(Value = "52")]
        M30_CAMERA = (52),

        [EnumMember(Value = "53")]
        M30T_CAMERA = (53),

        [EnumMember(Value = "61")]
        H20N = (61),

        [EnumMember(Value = "165")]
        DOCK_CAMERA = (165),

        [EnumMember(Value = "90742")]
        L1 = (90742),

        [EnumMember(Value = "66")]
        M3E_CAMERA = (66),

        [EnumMember(Value = "68")]
        M3M_CAMERA = (68),

        [EnumMember(Value = "80")]
        M3D_CAMERA = (80),

        [EnumMember(Value = "81")]
        M3TD_CAMERA = (81),

        [EnumMember(Value = "82")]
        H30 = (82),

        [EnumMember(Value = "83")]
        H30T = (83),

        /**
         * 辅助影像
         */
        [EnumMember(Value = "176")]
        AUX_M3D = (176),

        [EnumMember(Value = "176")]
        AUX_M3TD = (176),

        [EnumMember(Value = "98")]
        M4D_CAMERA = (98),

        [EnumMember(Value = "99")]
        M4TD_CAMERA = (99),


    }
    public enum DeviceSubTypeEnums
    {
        [EnumMember(Value = "0")]
        ZERO = (0),
        [EnumMember(Value = "1")]
        ONE = (1),
        [EnumMember(Value = "2")]
        TWO = (2),

        [EnumMember(Value = "65535")]
        _65535 = (65535),
    }
    public static class EnumExtensions
    {
        public static int ToInt(this Enum value)
        {
            return Convert.ToInt32(value);
        }

        public static string GetValue(this Enum value)
        {
            return GetEnumMemberValue(value);
        }

        /// <summary>
        /// 获取枚举值的 EnumMember Value
        /// </summary>
        public static string GetEnumMemberValue(this Enum value)
        {
            var field = value.GetType().GetField(value.ToString());
            var attribute = field.GetCustomAttribute<EnumMemberAttribute>();
            return attribute?.Value ?? value.ToString();
        }
        /// <summary>
        /// 根据 EnumMember Value 获取枚举值
        /// </summary>
        public static T? GetEnumFromEnumMemberValue<T>(string value) where T : struct, Enum
        {
            foreach (var field in typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static))
            {
                var attribute = field.GetCustomAttribute<EnumMemberAttribute>();
                if (attribute != null && attribute.Value == value)
                    return (T)field.GetValue(null);
            }

            // 如果没有找到匹配的 EnumMember，尝试按名称解析
            if (Enum.TryParse<T>(value, true, out var result))
                return result;

            return null;
        }

        /// <summary>
        /// 获取枚举值的描述
        /// </summary>
        public static string GetDescription(this Enum value)
        {
            var field = value.GetType().GetField(value.ToString());
            var attribute = field.GetCustomAttribute<DescriptionAttribute>();
            return attribute?.Description ?? value.ToString();
        }
    }

    public static class EnumHelper
    {
        public static T GetEnumFromDescription<T>(string description) where T : Enum
        {
            foreach (var field in typeof(T).GetFields())
            {
                if (Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))
                    is DescriptionAttribute attribute)
                {
                    if (attribute.Description == description)
                        return (T)field.GetValue(null);
                }
            }
            throw new ArgumentException("Not found", nameof(description));
        }

        /// <summary>
        /// 获取枚举值的 EnumMember Value
        /// </summary>
        public static string GetEnumMemberValue<T>(T value) where T : Enum
        {
            var field = value.GetType().GetField(value.ToString());
            var attribute = field.GetCustomAttribute<EnumMemberAttribute>();
            return attribute?.Value ?? value.ToString().ToLower();
        }

        /// <summary>
        /// 根据 EnumMember Value 获取枚举值
        /// </summary>
        public static T? GetEnumFromEnumMemberValue<T>(string value) where T : struct, Enum
        {
            foreach (var field in typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static))
            {
                var attribute = field.GetCustomAttribute<EnumMemberAttribute>();
                if (attribute != null && attribute.Value == value)
                    return (T)field.GetValue(null);
            }

            // 如果没有找到匹配的 EnumMember，尝试按名称解析
            if (Enum.TryParse<T>(value, true, out var result))
                return result;

            return null;
        }

        /// <summary>
        /// 获取枚举值的描述
        /// </summary>
        public static string GetDescription<T>(T value) where T : Enum
        {
            var field = value.GetType().GetField(value.ToString());
            var attribute = field.GetCustomAttribute<DescriptionAttribute>();
            return attribute?.Description ?? value.ToString();
        }
    }
}
